package com.kzd.btgame.main.mine

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.btgame.R
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.ActivityMineMessageBinding
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.mine.adapter.MineMessageAdapter
import com.kzd.btgame.main.toMessageDetailsActivity
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MineMessageActivity : OwnerViewBindingActivity<ActivityMineMessageBinding>() {

    private val mMineMessageAdapter = MineMessageAdapter()

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMineMessageBinding
        get() = ActivityMineMessageBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMineMessageBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMineMessageAdapter.apply {
            setOnItemClickListener { _, _, position ->
                val itemData = getItem(position)
                toMessageDetailsActivity(itemData)
            }
        }

        mViewBinding.flMineMessageBar.setOnClickRightTextView {
            mViewModel.mineMessageRead()
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.mineMessage()
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.mineMessage(isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.mineMessage()
    }

    override fun initViewMode() {

        mViewModel.userMessageLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mMineMessageAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMineMessageAdapter.addData(data)
            } else {
                mMineMessageAdapter.setNewInstance(data)
                mViewModel.mineMessageRead()
            }
        })

        mViewModel.mineMessageReadLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            mViewModel.userAppTips()
        })
    }
}